<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="108px"
    >
      <el-form-item label="项目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="节目平台" prop="name">
        <el-input
          v-model="queryParams.platform"
          placeholder="请输入节目平台"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目状态" prop="projectStatus">
        <el-select v-model="queryParams.projectStatus" placeholder="项目状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="isSystemAdmin">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:project:add']"
          >新增项目</el-button
        >
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList">
      <el-table-column align="center" type="index" width="80" />
      <!-- <el-table-column label="项目编号" align="center" prop="code" /> -->
      <el-table-column label="项目名称" align="center" prop="name" />
      <el-table-column label="节目平台" align="center" prop="platform" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <el-tag v-if="row.projectStatus == '0'">正常</el-tag>
          <el-tag type="danger" v-else>停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启用时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="280"
        v-if="isSystemAdmin"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:project:edit']"
            >修改</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleKeyOpen(scope.row)"
            v-hasPermi="['system:project:remove']"
            >编辑艺人填写字段</el-button
          >

          <el-button
            size="mini"
            type="text"
            style="color: red"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:project:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <edit ref="editForm" @update="getList" />
    <edit-keys ref="editKeysForm" @update="getList" />
  </div>
</template>

<script>
import { listProject, getProject, delProject } from '@/api/system/project'
import edit from './edit.vue'
import editKeys from './editKeys.vue'
import auth from '@/plugins/auth'

export default {
  name: 'Project',
  components: { edit, editKeys },
  dicts: ['sys_normal_disable'],
  data() {
    return {
      isSystemAdmin: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: '',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      projectList: [],
      keyTitle: '',

      keyOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: undefined,
        projectStatus: undefined
      }
    }
  },
  created() {
    this.isSystemAdmin = auth.hasRoleOr(['@systemAdmin'])
    this.getList()
  },
  methods: {
    /** 查询项目列表 */
    getList() {
      this.loading = true
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.editForm.open('添加项目', { projectStatus: '0' })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const projectId = row.id || this.ids
      getProject(projectId).then(response => {
        this.$refs.editForm.open('修改项目', response.data)
      })
    },

    /** 新增按钮操作 */
    handleKeyOpen(row) {
      console.log(row)
      this.$refs.editKeysForm.open(row)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.id || this.ids
      const projectName = row.name || this.names
      this.$modal
        .confirm('是否确认删除项目名称为"' + projectName + '"的数据项？')
        .then(function () {
          return delProject(projectIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    }
  }
}
</script>
